﻿--
--
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'dsZeusPickingList' AND type = 'P')
BEGIN
  DROP PROCEDURE dsZeusPickingList
END
GO

-- HU - Kiszedési lista
-- EN - Picking list
--
CREATE PROCEDURE dsZeusPickingList
(@AccessToken INTEGER)
 AS
BEGIN
  DECLARE
    @Err INTEGER,
    @Rows INTEGER,
    @UserID INTEGER,
    @Lang NVARCHAR(10)

  EXEC @Err = spAccessTokenCheck @AccessToken, @UserID OUTPUT, @Lang OUTPUT

  IF (@Err != 0)
    GOTO __ERROR__


  -- HU - Lefoglal egy új kiszedési listát vagy jó lesz egy már megkezdett
  -- EN - Get a new picking list for this user OR already existing
  --
  UPDATE ZeusPicking 
     SET UserID = @UserID
   WHERE ID = 
      (SELECT TOP 1 ID 
         FROM ZeusPicking 
        WHERE (Status = 'O') AND ((UserID IS NULL) OR (UserID = @UserID)))
    

  -- HU - Kiszedési lista adatai
  -- EN - Get the picking details
  --
  SELECT p.ID, p.Code, p.PartnerName,
         
         i.ID ItemID, i.Code ItemCode, i.LocationCode, i.ArticleCode, i.ArticleName, 

         CAST(i.Expired as DATE) Expired, i.Quantity, i.UnitCode, N'' Notes

    FROM ZeusPicking p
    JOIN ZeusPickingItem i ON p.ID = i.PickingID
   WHERE p.ID = 
      (SELECT TOP 1 ID 
         FROM ZeusPicking 
        WHERE (Status = 'O') AND ((UserID IS NULL) OR (UserID = @UserID)))

-- Well done.
--
  RETURN(0)

-- We have a problem !
--
__ERROR__:
  RETURN(1)
END
GO
